package gui;

import gui.TableModel;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.Dimension;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import types.Menu;
import types.MenuItem;

import java.awt.Font;
import java.awt.Color;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

public class FrmMenu extends JDialog implements ISubject
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField txtText = null;
	private JComboBox cbxLevel = null;
	private JButton btnAdd = null;
	private JButton btnDelete = null;
	private JButton btnOK = null;
	private JButton btnCancel = null;
	private JPanel jPanel = null;
	private JTextField txtItemText = null;
	private JTextField txtForm = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JComboBox cbxItemLevel = null;
	
	
	
	private Vector<IObserver> observers = null;

	/**
	 * @param owner
	 */
	public FrmMenu(/*Frame owner*/)
	{
		super(/*owner*/);
		observers = new Vector<IObserver>(1,1);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(288, 402);
		this.setContentPane(getJContentPane());
		this.setTitle("Menu");
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		setLocationRelativeTo(null);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(21, 43, 76, 16));
			jLabel1.setText("Access Level");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(21, 15, 33, 16));
			jLabel.setText("Text");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getTxtName(), null);
			jContentPane.add(getCbxLevel(), null);
			jContentPane.add(getBtnOK(), null);
			jContentPane.add(getBtnCancel(), null);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes txtName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtName()
	{
		if (txtText == null)
		{
			txtText = new JTextField();
			txtText.setBounds(new Rectangle(108, 11, 152, 25));
		}
		return txtText;
	}

	/**
	 * This method initializes cbxLevel	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCbxLevel()
	{
		if (cbxLevel == null)
		{
			cbxLevel = new JComboBox();
			cbxLevel.setBounds(new Rectangle(108, 39, 152, 25));
			cbxLevel.addItem("0");
			cbxLevel.addItem("1");
			cbxLevel.addItem("2");
			cbxLevel.addItem("3");
		}
		return cbxLevel;
	}

	/**
	 * This method initializes btnAdd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnAdd()
	{
		if (btnAdd == null)
		{
			btnAdd = new JButton();
			btnAdd.setText("Add");
			btnAdd.setBounds(new Rectangle(105, 95, 56, 26));
			btnAdd.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					final TableModel tableModel = (TableModel) jTable.getModel();
					int count = tableModel.getRowCount();
					tableModel.setRowCount(count + 1);
					tableModel.setValueAt(txtItemText.getText(),count,0);
					tableModel.setValueAt(Integer.parseInt(cbxItemLevel.getSelectedItem().toString()),count,1);
					tableModel.setValueAt(txtForm.getText(),count,2);
					
					jTable.repaint();
					
				}
			});
		}
		return btnAdd;
	}

	/**
	 * This method initializes btnDelete	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnDelete()
	{
		if (btnDelete == null)
		{
			btnDelete = new JButton();
			btnDelete.setText("Delete");
			btnDelete.setBounds(new Rectangle(187, 95, 70, 26));
			btnDelete.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					final TableModel tableModel = (TableModel) jTable.getModel();
					int slected = tableModel.getRow();
					
					if(slected<0)
						return;
					
//					for (int i = 0; i < tableModel.getRowCount(); i++)
//					{
//						
//					}
					
					tableModel.removeRow(slected);
					
					
					jTable.repaint();
				}
			});
		}
		return btnDelete;
	}

	/**
	 * This method initializes btnOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOK()
	{
		if (btnOK == null)
		{
			btnOK = new JButton();
			btnOK.setBounds(new Rectangle(137, 336, 51, 26));
			btnOK.setText("OK");
			btnOK.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					Vector<MenuItem> items = getMenuItems(); 
					
					Menu m = new Menu(
							Integer.parseInt(cbxLevel.getSelectedItem().toString()),
							txtText.getText(),
							items
							); 
					
					for(int i=0;i<observers.size();i++)
						observers.get(i).takeResult(m, "Menu");
					
					dispose();
					return;
				}

			});
		}
		return btnOK;
	}
	
	private Vector<MenuItem> getMenuItems()
	{
		final TableModel tableModel = (TableModel) jTable.getModel();
		Vector<MenuItem> vec = new Vector<MenuItem>();
		
		for (int i = 0; i < tableModel.getRowCount(); i++)
		{
			MenuItem item = new MenuItem(
					Integer.parseInt(tableModel.getValueAt(i, 1).toString()),
					tableModel.getValueAt(i, 0).toString(),
					tableModel.getValueAt(i, 2).toString()
					);
			
			vec.addElement(item);
		}
		
		return vec;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel()
	{
		if (btnCancel == null)
		{
			btnCancel = new JButton();
			btnCancel.setBounds(new Rectangle(195, 336, 73, 26));
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					dispose();
					return;
				}
			});
			
		}
		return btnCancel;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel()
	{
		if (jPanel == null)
		{
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(18, 68, 74, 16));
			jLabel5.setText("Form to open");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(18, 43, 76, 16));
			jLabel4.setText("Access Level");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(18, 19, 25, 16));
			jLabel3.setText("Text");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(4, 77, 269, 249));
			jPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Menu Items", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanel.add(getBtnAdd(), null);
			jPanel.add(getBtnDelete(), null);
			jPanel.add(getTxtText(), null);
			jPanel.add(getTxtForm(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(jLabel4, null);
			jPanel.add(jLabel5, null);
			jPanel.add(getJScrollPane(), null);
			jPanel.add(getCbxItemLevel(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes txtText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtText()
	{
		if (txtItemText == null)
		{
			txtItemText = new JTextField();
			txtItemText.setBounds(new Rectangle(105, 17, 152, 20));
		}
		return txtItemText;
	}

	/**
	 * This method initializes txtForm	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtForm()
	{
		if (txtForm == null)
		{
			txtForm = new JTextField();
			txtForm.setBounds(new Rectangle(105, 66, 152, 20));
		}
		return txtForm;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane()
	{
		if (jScrollPane == null)
		{
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(8, 127, 252, 113));
			jScrollPane.setViewportView(getTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes table	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTable()
	{
		if (jTable == null)
		{
			final TableModel tableModel = new TableModel();
			tableModel.addColumn("Text");
			tableModel.addColumn("Level");
			tableModel.addColumn("Form");
			tableModel.setRowCount(0);
			tableModel.initTableModel();

			jTable = new JTable(tableModel);
			jTable.setCellSelectionEnabled(false);
			jTable.setRowSelectionAllowed(true);
			jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jTable.setColumnSelectionAllowed(false);
			jTable.addMouseListener(new java.awt.event.MouseAdapter()
			{
				public void mousePressed(java.awt.event.MouseEvent e)
				{
					tableModel.setRow(jTable.rowAtPoint(new Point(e.getX(),e.getY())));
				}
			});
		}
		return jTable;
	}

	/**
	 * This method initializes cbxItemLevel	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCbxItemLevel()
	{
		if (cbxItemLevel == null)
		{
			cbxItemLevel = new JComboBox();
			cbxItemLevel.setBounds(new Rectangle(105, 39, 152, 25));
			cbxItemLevel.addItem("0");
			cbxItemLevel.addItem("1");
			cbxItemLevel.addItem("2");
			cbxItemLevel.addItem("3");
		}
		return cbxItemLevel;
	}

	@Override
	public void registerInterest(IObserver obs)
	{
		observers.addElement(obs);
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
